package com.link.linkupjavaweb.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.link.linkupjavaweb.entity.UserItem;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 用户道具Mapper接口
 */
@Mapper
public interface UserItemMapper extends BaseMapper<UserItem> {
    
    /**
     * 查询用户的所有道具
     */
    @Select("SELECT ui.*, gi.item_code, gi.item_name, gi.icon_url " +
            "FROM tb_user_item ui " +
            "LEFT JOIN tb_game_item gi ON ui.item_id = gi.id " +
            "WHERE ui.user_id = #{userId}")
    List<UserItem> selectUserItemsWithDetails(@Param("userId") Long userId);
    
    /**
     * 根据用户ID和道具代码查询
     */
    @Select("SELECT ui.* FROM tb_user_item ui " +
            "LEFT JOIN tb_game_item gi ON ui.item_id = gi.id " +
            "WHERE ui.user_id = #{userId} AND gi.item_code = #{itemCode}")
    UserItem selectByUserIdAndItemCode(@Param("userId") Long userId, @Param("itemCode") String itemCode);
    
    /**
     * 减少道具数量
     */
    @Update("UPDATE tb_user_item SET item_count = item_count - 1 " +
            "WHERE user_id = #{userId} AND item_id = #{itemId} AND item_count > 0")
    int decreaseItemCount(@Param("userId") Long userId, @Param("itemId") Long itemId);
}

